"""
Celery应用配置
用于异步任务和定时任务
"""
from celery import Celery
from celery.schedules import crontab
from config.settings import settings

# 创建Celery应用
celery_app = Celery(
    "xiaohongshu_app",
    broker=f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/1",  # 使用Redis作为消息代理
    backend=f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/2"   # 使用Redis作为结果后端
)

# Celery配置
celery_app.conf.update(
    task_serializer='json',
    accept_content=['json'],
    result_serializer='json',
    timezone='Asia/Shanghai',
    enable_utc=True,
    # 定时任务配置
    beat_schedule={
        'build-recommendation-index': {
            'task': 'tasks.recommendation.build_recommendation_index',
            'schedule': 60.0,  # 每分钟执行一次
        },
        # 每分钟清理一次超时未支付订单（兜底清理）
        'cleanup-unpaid-orders': {
            'task': 'tasks.orders.cleanup_unpaid_orders',
            'schedule': 60.0,
        },
    },
)

